package com.bojiu.lawsearch.lawsearch.wxzf;

import com.bojiu.lawsearch.lawsearch.util.JsonResult;
import com.bojiu.lawsearch.lawsearch.util.Rcode;
import com.bojiu.lawsearch.lawsearch.util.ResultCode;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

/**
 * @author: afdiao
 * @Date：2020/3/31 14:58
 * @Description:
 */
@Component
@Slf4j
public class WXZFService {

    @Autowired
    SubmitOrderHandler submitOrderHandler;

    @Autowired
    WXZFHttpHandler wxzfHttpHandler;

    /**
     * 获取微信支付二维码
     *
     * @param fee     支付金额
     * @param body    商品描述
     * @param request
     * @return
     */
    public JsonResult nativeOrder(int fee, String body, HttpServletRequest request) {
        try {
            String orderNum = UUID.randomUUID().toString().replaceAll("-", "");
            Map<String, String> resultMap = submitOrderHandler.onlySubmitOrder(null,
                    body, "NATIVE", "test", orderNum, fee, request, "1561645845861", "123");
            if (resultMap.containsKey("false")) {
                return new JsonResult(ResultCode.FAIL);
            }
            if (!resultMap.containsKey("code_url") || StringUtils.isEmpty(resultMap.get("code_url"))) {
                log.error("code_url is null");
                return new JsonResult(ResultCode.FAIL);
            }
            String code_url = resultMap.get("code_url");
            log.debug("code_url is :{}", code_url);
            // 链接转成二维码
            String rcodetoBase64 = Rcode.RcodetoBase64(200, 200, code_url);
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("code", rcodetoBase64);
            jsonObject.addProperty("orderNum", orderNum);
            return new JsonResult(ResultCode.SUCCESS, jsonObject);
        } catch (Exception e) {
            log.error("error to nativeSub,error is :{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }

    public JsonResult refund(String orderNum, int total_fee, int refund_fee) {
        try {
            String nonce_str = WXZFUtil.generateNonceStr();
            String out_refund_no = String.valueOf(System.currentTimeMillis() / 1000);
            Map<String, String> refundmap = WXZFUtil.createRefundData(WXZFConstance.GZ_APPID, nonce_str, null,
                    orderNum, out_refund_no, String.valueOf(total_fee), String.valueOf(refund_fee), WXZFConstance.INFORMURL);
            Map<String, String> doPostSSL = wxzfHttpHandler.doPostSSL(WXZFUtil.mapToXml(refundmap),
                    WXZFConstance.REFUNDURL);
            log.debug("refund result is :{}", doPostSSL);
            return new JsonResult(ResultCode.SUCCESS, doPostSSL);
        } catch (Exception e) {
            log.error("error to refund,error is :{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }

    /**
     * 企业付款
     *
     * @param openid
     * @param outTradeNo
     * @param amount
     * @param desc
     * @param ip
     * @return
     */
    public JsonResult transfers(String openid, String outTradeNo, int amount, String desc, String ip){
        if (amount < WXZFConstance.TRANSFERS_MIN) {
            return new JsonResult(ResultCode.FAIL);
        }
        try {
            Map<String, String> transfersData = WXZFUtil.createTransfersData(openid, outTradeNo, String.valueOf(amount), desc, ip);
            Map<String, String> doPostSSL = wxzfHttpHandler.doPostSSL(WXZFUtil.mapToXml(transfersData),
                    WXZFConstance.REFUNDURL);
            log.debug("refund result is :{}", doPostSSL);
            if (WXZFConstance.SUCCESS.equals(doPostSSL.get("return_code")) && WXZFConstance.SUCCESS.equals(doPostSSL.get("result_code"))) {
                return new JsonResult(ResultCode.SUCCESS, doPostSSL);
            }else {
                return new JsonResult(ResultCode.FAIL);
            }
        } catch (Exception e) {
            log.error("error to refund,error is :{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }
}
